package com.alogic.es.xscript.search;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import org.apache.commons.lang3.StringUtils;

/**
 * query by exist
 *
 * @since 1.6.13.9 [20200824 duanyy]
 */
public class EsExist extends AbstractLogiclet {
    protected String $field;

    public EsExist(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $field = PropertiesConstants.getRaw(p,"field",$field);
    }

    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String field = PropertiesConstants.transform(ctx,$field,"");
        if (StringUtils.isNotEmpty(field)) {
            XsObject query = current.getObjectChild("exists", true);
            query.addProperty("field",field);
        }
    }
}